package com.kwin.demo.server.framework.util.datadic.handler;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

/**
 * @author kwin
 * @Date 2022/2/15 15:52
 **/
public class FieldSpy<T> {
    public boolean[][] b = { { false, false }, { true, true } };
    public String name = "Alice";
    public List<String[]> list;

    public List<String>[] aaa;
    public String[][] bbb;
    public T val;

    public static void main(String... args) {
        try {
            FieldSpy tt = new FieldSpy<String>();
            Class<?> c = tt.getClass();
            Field f = c.getField("aaa");
            System.out.format("Type: %s%n", f.getType());
            System.out.format("GenericType: %s%n", f.getGenericType());
            System.out.println("---");
            System.out.println(f.getType().isArray());
//            System.out.println(f.getType().getComponentType());
//            System.out.println(f.getType().getComponentType().isArray());
//            System.out.println(f.getType().getComponentType().getComponentType());
            System.out.println(f.getGenericType());
            System.out.println(f.getType().getComponentType());
            System.out.println(f.getType().getComponentType());
            System.out.println(Collection.class.isAssignableFrom(f.getType().getComponentType()));
            ParameterizedType stringListType =  (ParameterizedType) f.getGenericType();

            if(stringListType.getActualTypeArguments()[0] instanceof Class) {
                System.out.println("class");
                System.out.println(((Class<?>)stringListType.getActualTypeArguments()[0]).isArray());
            }

            if(stringListType.getActualTypeArguments()[0] instanceof ParameterizedType) {
                System.out.println("ParameterizedType");
                System.out.println(((ParameterizedType) stringListType.getActualTypeArguments()[0]).getActualTypeArguments()[0]);
                Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0];
                System.out.println(stringListClass);
            }



            // production code should handle these exceptions more gracefully
        } catch (NoSuchFieldException x) {
            x.printStackTrace();
        }
    }
}
